/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditWaresFrame.java
 *
 * Created on Jan 19, 2010, 12:41:43 PM
 */

package org.acmwustl.bubble.gui;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.ArrayList;
import org.acmwustl.bubble.controllers.VirtualKeyboardInterface;
import org.acmwustl.bubble.logic.ConfigManager;
import org.acmwustl.bubble.logic.Item;
import org.acmwustl.bubble.windowutils.WindowUtils;

/**
 *
 * @author officer
 */
public class EditWaresFrame extends javax.swing.JFrame {

    private final int numRows = 6;
    private ArrayList<Item> wares;

    MainWindow parent;
    
    /** Creates new form EditWaresFrame */
    public EditWaresFrame(MainWindow mw) {
        initComponents();

        parent = mw;

        setSize(new Dimension(1024,768));
        WindowUtils.centerWindow(this);

        wares = ConfigManager.getInstance().getWares();

        int numColumns = wares.size() / numRows + 1;
        waresPanel.setLayout(new GridLayout(numRows, numColumns));

        for(Item i : wares) {
            waresPanel.add(new ItemDetailPanel(this, i));
        }

        virtualKeyboard1.setEnabled(false);
        virtualNumpad1.setEnabled(false);
    }

    public void deleteWare(Item i) {
        wares.remove(i);
        ConfigManager.getInstance().updateItems(wares);

        EditWaresFrame ewf = new EditWaresFrame(parent);
        ewf.setVisible(true);
        dispose();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        waresPanel = new javax.swing.JPanel();
        virtualKeyboard1 = new org.acmwustl.bubble.gui.keyboards.VirtualKeyboard();
        virtualNumpad1 = new org.acmwustl.bubble.gui.keyboards.VirtualNumpad();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setUndecorated(true);

        waresPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Wares"));

        javax.swing.GroupLayout waresPanelLayout = new javax.swing.GroupLayout(waresPanel);
        waresPanel.setLayout(waresPanelLayout);
        waresPanelLayout.setHorizontalGroup(
            waresPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 924, Short.MAX_VALUE)
        );
        waresPanelLayout.setVerticalGroup(
            waresPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 222, Short.MAX_VALUE)
        );

        jButton1.setText("DONE");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("ADD");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(waresPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(virtualKeyboard1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(virtualNumpad1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton2});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(waresPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(virtualNumpad1, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(virtualKeyboard1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        ConfigManager.getInstance().updateItems(wares);
        parent.dispose();
        MainWindow mw = new MainWindow();
        mw.setVisible(true);
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        wares.add(new Item("New Ware", 0));
        ConfigManager.getInstance().updateItems(wares);

        EditWaresFrame ewf = new EditWaresFrame(parent);
        ewf.setVisible(true);
        dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    public void setKeyboardFocus(VirtualKeyboardInterface vki) {
        virtualKeyboard1.setVirtualKeyboardInterface(vki);
        virtualNumpad1.setVirtualKeyboardInterface(null);
    }
    
    public void setNumpadFocus(VirtualKeyboardInterface vki) {
        virtualNumpad1.setVirtualKeyboardInterface(vki);
        virtualKeyboard1.setVirtualKeyboardInterface(null);
    }

    public boolean isKeyboardFocus(VirtualKeyboardInterface vki) {
        if(virtualKeyboard1.getFocusedWidget() == vki)
            return true;
        return false;
    }

    public boolean isNumpadFocus(VirtualKeyboardInterface vki) {
        if(virtualNumpad1.getVirtualKeyboardInterface() == vki)
            return true;
        return false;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private org.acmwustl.bubble.gui.keyboards.VirtualKeyboard virtualKeyboard1;
    private org.acmwustl.bubble.gui.keyboards.VirtualNumpad virtualNumpad1;
    private javax.swing.JPanel waresPanel;
    // End of variables declaration//GEN-END:variables

}
